package rpc.intf.impl.loadBalanceAlgorithm;

import com.alibaba.nacos.api.naming.pojo.Instance;
import rpc.intf.LoadBalancer;

import java.util.List;

/**
 * 转轮算法
 */
public class RoundRobinLoadBalancer implements LoadBalancer {


    private int index = 0;

    @Override
    public Instance select(List<Instance> instances) {
        int num_server = instances.size();
        index = (index >= num_server) ? (index % num_server): index;
        return instances.get(index++);
    }
}
